import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
import axios from 'axios'
import { message } from 'antd'
import 'antd/dist/reset.css'

// 声明全局 baseUrl 类型
declare global {
  interface Window {
    baseUrl: string
  }
}

window.baseUrl = 'https://zkapi.lvseguanjia.cn'

axios.defaults.baseURL = 'https://zkapi.lvseguanjia.cn'

// 增加token Authorization
axios.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (localStorage.getItem('token')) {
    config.headers.Authorization = token
  }
  return config
})

// 校验401清除token
axios.interceptors.response.use(response => {
  return response
}, error => {
  console.log('error', error)
  if (error.status === 401 && localStorage.getItem('token')) {
    localStorage.removeItem('token');
    message.error('登录已过期，请重新登录')
    window.location.reload()
  }
  return Promise.reject(error)
})



createRoot(document.getElementById('root')!).render(
  <App />
)
